<template>
  <div class="app-container calendar-list-container">
    <el-form :model="form" ref="form" label-width="100px">
      <el-form-item label="模板说明图*">
        <el-upload
          class="avatar-uploader"
          name="imageFile"
          :headers="requestHeader"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :data="uploadParam"
        >
          <img v-if="form.componentImage" :src="imgBaseURL + form.componentImage" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过2MB</div>
        </el-upload>
      </el-form-item>
  
      <el-form-item label="推荐位模板名称*" prop="groupName">
        <el-input v-model="form.componentName" placeholder="推荐位模板名称"></el-input>
      </el-form-item>
  
      <el-form-item label="状态*">
        <template>
          <el-radio v-model="form.componentStatus" label="1">有效</el-radio>
          <el-radio v-model="form.componentStatus" label="0">无效</el-radio>
        </template>
      </el-form-item>
      <div class="dialog-footer">
        <el-button type="primary" @click="cateGroup('form')" id="shopsJuLi">确 定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getToken } from "utils/auth";
import { updateShopsComponent,getComponentById } from "api/shop/shops/index";
import globalVariable from '@/utils/global'
export default {
  name: "addComponenManage",
  data() {
    return {
      imgBaseURL: this.GLOBAL.imgBaseURL,
      requestHeader: { token: getToken() },
      uploadImgUrl: this.GLOBAL.uploadImgUrl,
      uploadParam: { useType: "component" },
      region: [],
      form: {
        componentId: undefined,
        componentImage: undefined,
        componentName:undefined,
        componentStatus: undefined,
        shopId: undefined,  
      },
    };
  },
  created() {
    this.getComponeninfo();
  },
  computed: {
    ...mapGetters(["elements"])
  },
  methods: {
    getComponeninfo(){
      this.form.shopId=this.$route.query.shopId;
      getComponentById(this.$route.query.id).then(response=>{
        this.form=response.data;
        this.form.componentStatus=response.data.componentStatus.toString();
      })
    },
    handleAvatarSuccess(res, file) {
      this.form.componentImage = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error("上传图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return (isJPG || isPNG) && isLt2M;
    },
    cateGroup(form) {
      if (this.form.componentImage == "" || this.form.componentImage == null) {
        this.$message.error("模板说明图不能为空");
        return false;
      }
      if (this.form.componentName == "" || this.form.componentName == null) {
        this.$message.error("推荐位模板名称不能为空");
        return false;
      }
      if(this.form.componentStatus=="" || this.form.componentStatus==null){
         this.$message.error("状态不能为空");
        return false;
      }
      const set = this.$refs;
      set[form].validate(valid => {
        if (valid) {
          this.form.shopId=globalVariable.shopId;
          updateShopsComponent(this.form).then(response => {
            if (response.message == 200) {
              this.$notify({
                title: "成功",
                message: "修改成功!",
                type: "success",
                duration: 2000
              });
            this.$router.push({path:'/shop/shops/managementShops',query:{id: this.form.shopId}});
            } else {
              this.$notify({
                title: "失败",
                message: "数据出现异常!",
                type: "error",
                duration: 2000
              });
             this.$router.push({path:'/shop/shops/managementShops',query:{id: this.form.shopId}});
            }
          });
        }
      });
    }
  }
};
</script>
<style>

#shopsJuLi {
  margin-left: 130px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
